package com.fingard.dsp.bank.directbank.vbao02.util;

/** 
 * @author 作者 E-mail:lirp@chinavbao.com 
 * @version 1.0
 * 类说明 
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;

import javax.net.ssl.HttpsURLConnection;

public class VbaoChargePostHandler {
	private static final List<String> not_sign_attrs = new ArrayList<String>();
	static {
		not_sign_attrs.add("Sign");
		not_sign_attrs.add("SignType");
		not_sign_attrs.add("BatchBankCode");
		not_sign_attrs.add("ResultCode");
		not_sign_attrs.add("ErrCode");
		not_sign_attrs.add("Description");
	}

	private List<String> getSortKey(SortedMap<String, ?> receiveData) {
		List<String> attrs = new ArrayList<String>();
		for (String key : receiveData.keySet()) {
			attrs.add(key);
		}
		Arrays.sort(attrs.toArray(new Object[0]));
		return attrs;
	}

	private byte[] getMD(byte[] inByte) {
		if (inByte == null) {
			return null;
		}
		byte[] hash = null;
		try {
			MessageDigest tMessageDigest = MessageDigest.getInstance("MD5");
			tMessageDigest.update(inByte);
			hash = tMessageDigest.digest();
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return hash;
	}

	/*
	 * 3.计算sign值
	 */
	private String getSignContent(SortedMap<String, ?> receiveData) {
		try {
			List<String> keys = getSortKey(receiveData);

			StringBuffer signContent = new StringBuffer("");

			for (String key : keys) {
				Object obj = receiveData.get(key);
				if (!(obj instanceof String)) {
					continue;
				}
				String content = (String) obj;

				if ((content == null) || (content.length() <= 0) || (not_sign_attrs.contains(key))
						|| content.equals("null") || ("").equals(content))
					continue;
				if ((signContent != null) && (signContent.length() > 0)) {
					signContent.append("&");
				}

				if ((content != null) && (content.trim().length() > 0)) {
					signContent.append(key).append("=").append(content);
				}
			}
			System.out.println("签名:" + signContent.toString());
			return signContent.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 4.MD5加密
	 */
	private String getStrMD(String str, String InputCharset) {
		if (str == null)
			return null;
		byte[] inByte = null;
		try {
			inByte = str.getBytes(InputCharset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] outByte = getMD(inByte);
		return byteToStr(outByte);
	}

	private String byteToStr(byte[] b) {
		if (b == null)
			return null;
		String hs = "";
		for (int n = 0; n < b.length; n++) {
			int v = b[n] & 0xFF;
			if (v < 16)
				hs = hs + "0";
			hs = hs + Integer.toString(v, 16).toUpperCase();
		}
		return hs;
	}

	public String http(String url, String reqmsg, String custNO, String sysId) {
		if (null == url || "".equals(url.trim())) {
			System.out.println("发送地址为空");
			return null;
		}
		URL u = null;
		HttpURLConnection con = null;
		StringBuffer strBuff = new StringBuffer();
		try {
			u = new URL(url);
			con = (HttpURLConnection) u.openConnection();
			con.setRequestMethod("POST");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setConnectTimeout(120000);
			con.setReadTimeout(120000);
			con.setUseCaches(false);
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
			strBuff.append("reqmsg=").append(reqmsg).append("&custNo=").append(custNO).append("&sysId=").append(sysId);
			osw.write(strBuff.toString());
			osw.flush();
			osw.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				con.disconnect();
			}

		}

		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
			String temp;
			while ((temp = br.readLine()) != null) {
				buffer.append(temp);
				buffer.append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return buffer.toString();
	}

	/**
	 * https协议
	 * 
	 * @param httpsUrl
	 * @param xmlStr
	 * @param InputCharset
	 * @return
	 */
	public String postHttps(String httpsUrl, String xmlStr, String InputCharset) {
		if (httpsUrl == null || httpsUrl == "") {
			return null;
		}

		if (xmlStr == null || xmlStr == "") {
			return null;
		}
		HttpsURLConnection urlCon = null;
		try {
			urlCon = (HttpsURLConnection) (new URL(httpsUrl)).openConnection();

			// HttpsURLConnection.setDefaultHostnameVerifier(hv);
			urlCon.setDoInput(true);
			urlCon.setDoOutput(true);
			urlCon.setRequestMethod("POST");
			// urlCon.setHostnameVerifier(new HostnameVerifier() {
			// @Override
			// public boolean verify(String hostname, SSLSession session) {
			// return true;
			// }
			// });
			urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			urlCon.setRequestProperty("Content-Length", String.valueOf(xmlStr.getBytes().length));
			urlCon.setUseCaches(false);
			// 设置为gbk可以解决服务器接收时读取的数据中文乱码问题
			urlCon.getOutputStream().write(xmlStr.getBytes(InputCharset));

			urlCon.getOutputStream().flush();
			urlCon.getOutputStream().close();
			BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream(), InputCharset));
			StringBuffer strBuff = new StringBuffer();
			String line;
			while ((line = in.readLine()) != null) {
				strBuff.append(line);
			}
			return strBuff.toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 讲XML进行RSA加密
	 * 
	 * @throws Exception
	 */
	private String converterRSA(SortedMap<String, String> dataMap, String InputCharset, String publicKeyPath)
			throws Exception {
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		strBuilder.append("<TRequest>");
		Set<String> objSet = dataMap.keySet();
		for (String key : objSet) {
			if (key == null || "null".equals(key) || "".equals(key)) {
				continue;
			}
			strBuilder.append("<").append(key.trim()).append(">");
			strBuilder.append(dataMap.get(key) == null ? "" : dataMap.get(key).trim());
			strBuilder.append("</").append(key.trim()).append(">");
		}
		strBuilder.append("</TRequest>");
		System.out.println("加密前：" + strBuilder.toString());

		// 使用服务端公钥进行RSA加密
		PublicKey publicKey = RSAUtil.getRSAPublicKeyByFileSuffix(publicKeyPath, "pem", "RSA");
		byte[] plainBytes = strBuilder.toString().getBytes("utf-8");
		String encrtptStr = Base64ForRSA
				.encode(RSAUtil.encrypt(plainBytes, publicKey, 2048, 11, "RSA/ECB/PKCS1Padding"));
		System.out.println("encode前的值:" + encrtptStr);
		String xml = "reqmsg=" + URLEncoder.encode(URLEncoder.encode(encrtptStr));// http设置的application/x-www-form-urlencoded则需要encode2次
		// String xml = "reqmsg="+ URLEncoder.encode(encrtptStr);
		return xml;
	}

}
